package pe.edu.pucp.cpnc.mm.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlType;

//Establece a Map, como la raiz del Xml
@XmlRootElement
//Indicamos en que orden queremos que imprima los atributos
@XmlType(propOrder ={"id","name","author","sizeX","sizeY","dateCreated",
		"dateModified","status","players","tiles"})

public class Map implements Serializable {
	private int id;
	private String name;
	private int sizeX;
	private int sizeY;
	private TileDetail[][] tiles;
	//private ArrayList<ArrayList<TileDetail>> tiles;
	private ArrayList<Player> players;
	private Date dateCreated;
	private Date dateModified;
	private String author;
	private int status;
	
	public Map(){	
		players = new ArrayList<Player>();	
	}
	
	public Map(int sizeX, int sizeY, String name,Tile tile){
		this.sizeX = sizeX;
		this.sizeY = sizeY;
		this.name = name;
		tiles = new TileDetail[sizeY][sizeX];
		
		for(int i=0;i<sizeY;i++){
			for (int j=0;j<sizeX;j++){
				TileDetail tileDetail = new TileDetail(tile,i,j);
				tiles[i][j] = tileDetail;				
			}
		}
	    players = new ArrayList<>();
	  //Se inicializa el Player 0, que tiene los objetos neutrales
	    Player p = new Player();
	    p.setId(0);
	    p.setColor(16777215);
	    p.setType(0);
	    p.setName("MapOwner");
	    players.add(p.getId(), p);
	}
        
        public Map(Map oMap){
            long timeIni = System.currentTimeMillis();
            id = oMap.id;
            name = oMap.name;
            sizeX = oMap.sizeX;
            sizeY = oMap.sizeY;
            author = oMap.author;
            dateCreated = oMap.dateCreated;
            dateModified = oMap.dateModified;
            status = oMap.status;
            
            HashMap<String, ObjectDetail> objDetMap = new HashMap<>();
            players = new ArrayList<>();
            for(Player p : oMap.players)
                players.add(new Player(p,objDetMap));
            
            tiles = new TileDetail[sizeY][sizeX];
            for(int i=0;i<sizeY;i++){
                for (int j=0;j<sizeX;j++){
                    TileDetail originalTile = oMap.tiles[i][j];
                    ObjectDetail oDetRef;
                    
                    if(originalTile.getObjInside()!=null)
                        oDetRef = objDetMap.get(originalTile.getObjInside().getId());
                    else
                        oDetRef = null;
                    
                    TileDetail tileDetail = new TileDetail(originalTile,oDetRef);
                    tiles[i][j] = tileDetail;				
                }
            }
            System.out.println("Tiempo de copia: " + (System.currentTimeMillis() - timeIni));
            
        }

	public int getId() {
		return id;
	}

	@XmlAttribute
	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getSizeX() {
		return sizeX;
	}

	public void setSizeX(int sizeX) {
		this.sizeX = sizeX;
	}

	public int getSizeY() {
		return sizeY;
	}

	public void setSizeY(int sizeY) {
		this.sizeY = sizeY;
	}
	
	
	/*public ArrayList<ArrayList<TileDetail>> getTiles() {
		return tiles;
	}
		
	public void setTiles(ArrayList<ArrayList<TileDetail>> tiles) {
		this.tiles = tiles;
	}*/
	
	public ArrayList<Player> getPlayers() {
		return players;
	}

	//Establecemos un wrapper para coleccion de players, tema estetico
	@XmlElementWrapper
	@XmlElement(name = "player")
	public void setPlayers(ArrayList<Player> players) {
		this.players = players;
	}

	@XmlElementWrapper (name = "tilesDetails")
	@XmlElement(name = "tileDetails")
	public TileDetail[][] getTiles() {
		return tiles;
	}

	public void setTiles(TileDetail[][] tiles) {
		this.tiles = tiles;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	public Date getDateModified() {
		return dateModified;
	}

	public void setDateModified(Date dateModified) {
		this.dateModified = dateModified;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}


	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}
	
	
	}
